// component/datetime/index.js
/**
 * 一个自定义的日期加时间选择器
 */
import tool from '../../utils/tools.js'
var date = new Date()
var currentHours = date.getHours()
var currentMinute = date.getMinutes()
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    multiArray: {
      type: Array,
      value: []
    },
    multiIndex: {
      type: Array,
      value: [0, 0, 0]
    },
    selectDate: {
      type: String,
      value: ''
    },
    placeholder: {
      type: String,
      value: ''
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    multiArray: [],
    multiIndex: [0, 0, 0],
    selectDate: '',
    placeholder: ''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 初始化控件
     * curDate 默认值
     */
    initData(curDate, placeholder) {
      if (currentHours + 1 > 23) {
        const dateStr = tool.formatTime("Y-M-D h:m:s", 0, 1)
        date = new Date(dateStr)
      } else {
        currentHours += 1
      }
      this.setData({
        selectDate: curDate,
        placeholder: placeholder
      })
      this.pickerTap()
    },
    /**
     * 获取值
     */
    getDate() {
      return this.data.selectDate
    },
    pickerTap: function() {
      date = new Date();
      var curDate = null
      if (this.data.selectDate != "") {
        curDate = new Date(this.data.selectDate)
      }
      var monthDay = ['今天', '明天'];
      var hours = [];
      var minute = [];
      currentHours = date.getHours();
      currentMinute = date.getMinutes();

      // 月-日
      for (var i = 0; i <= 90; i++) {
        var date1 = new Date();
        date1.setDate(date.getDate() + i);
        var md = (date1.getMonth() + 1) + "-" + date1.getDate();
        if (i > 1) {
          monthDay.push(md);
        }
        if (curDate != null &&
          curDate.getFullYear() == date1.getFullYear() &&
          curDate.getMonth() + 1 == date1.getMonth() + 1 &&
          curDate.getDate() == date1.getDate()) {
          this.data.multiIndex[0] = i
        }
      }
      var data = {
        multiArray: this.data.multiArray,
        multiIndex: this.data.multiIndex
      };

      if (data.multiIndex[0] === 0) {
        if (data.multiIndex[1] === 0) {
          this.loadDate(hours, minute);
        } else {
          this.loadMinute(hours, minute);
        }
      } else {
        this.loadHoursMinute(hours, minute);
      }

      data.multiArray[0] = monthDay;
      data.multiArray[1] = hours;
      //设置小时默认值
      if (curDate != null) {
        for (const h in hours) {
          const hour = hours[h] + ""
          const curHour = curDate.getHours() < 10 ? "0" + curDate.getHours() : curDate.getHours() + ""
          if (curHour == hour) {
            data.multiIndex[1] = parseInt(h)
          }
        }
      }
      data.multiArray[2] = minute;
      //设置分默认值
      if (curDate != null) {
        for (const m in minute) {
          const mm = minute[m]
          const curMM = curDate.getMinutes() < 10 ? "0" + curDate.getMinutes() : curDate.getMinutes() + ""
          if (curMM + "" == mm) {
            data.multiIndex[2] = parseInt(m)
          }
        }
      }
      this.setData(data);
    },
    bindMultiPickerColumnChange: function(e) {
      date = new Date();
      var that = this;

      var monthDay = ['今天', '明天'];
      var hours = [];
      var minute = [];

      currentHours = date.getHours();
      currentMinute = date.getMinutes();
      var data = {
        multiArray: this.data.multiArray,
        multiIndex: this.data.multiIndex
      };
      // 把选择的对应值赋值给 multiIndex
      data.multiIndex[e.detail.column] = e.detail.value;

      // 然后再判断当前改变的是哪一列,如果是第1列改变
      if (e.detail.column === 0) {
        // 如果第一列滚动到第一行
        if (e.detail.value === 0) {
          that.loadDate(hours, minute);

        } else {
          that.loadHoursMinute(hours, minute);
        }

        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        // 如果是第2列改变
      } else if (e.detail.column === 1) {

        // 如果第一列为今天
        if (data.multiIndex[0] === 0) {
          if (e.detail.value === 0) {
            that.loadDate(hours, minute);
          } else {
            that.loadMinute(hours, minute);
          }
          // 第一列不为今天
        } else {
          that.loadHoursMinute(hours, minute);
        }
        data.multiIndex[2] = 0;

        // 如果是第3列改变
      } else {
        // 如果第一列为'今天'
        if (data.multiIndex[0] === 0) {

          // 如果第一列为 '今天'并且第二列为当前时间
          if (data.multiIndex[1] === 0) {
            that.loadDate(hours, minute);
          } else {
            that.loadMinute(hours, minute);
          }
        } else {
          that.loadHoursMinute(hours, minute);
        }
      }
      data.multiArray[1] = hours;
      data.multiArray[2] = minute;
      this.setData(data);
    },
    loadHoursMinute: function(hours, minute) {
      // 时
      for (var i = 9; i < 24; i++) {
        hours.push(i < 10 ? "0" + i : i)
      }
      // 分
      for (var i = 0; i < 60; i += 5) {
        minute.push(i < 10 ? "0" + i : i)
      }
    },
    loadMinute: function(hours, minute) {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i < 10 ? "0" + i : i)
      }
      // 分
      for (var i = 0; i < 60; i += 5) {
        minute.push(i < 10 ? "0" + i : i)
      }
    },
    /**
     * 获取时间
     */
    bindStartMultiPickerChange: function(e) {
      var that = this;
      var monthDay = tool.formatTime("Y-M-D", 0, e.detail.value[0]);
      var hours = that.data.multiArray[1][e.detail.value[1]];
      var minute = that.data.multiArray[2][e.detail.value[2]];
      var startDate = monthDay + " " + hours + ":" + minute + ":00";
      that.setData({
        selectDate: startDate
      })
    },
    loadDate: function(hours, minute) {
      // 时
      for (var i = currentHours; i < 24; i++) {
        hours.push(i < 10 ? "0" + i : i)
      }
      // 分
      for (var i = currentMinute; i < 60; i += 5) {
        minute.push(i < 10 ? "0" + i : i)
      }
    }
  }
})